package com.zrrd.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.zrrd.reggie.bean.AddressBook;
import com.zrrd.reggie.service.AddressBookService;
import com.zrrd.reggie.util.BaseContext;
import com.zrrd.reggie.util.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/addressBook")
public class AddressBookController {
    @Autowired
    private AddressBookService addressBookService;

    //添加收货地址
    @PostMapping
    public R<String> add(@RequestBody AddressBook addressBook) {
        //新增地址之前 查询一下 该用户是否有地址
        QueryWrapper<AddressBook> wrapper = new QueryWrapper<>();
        wrapper.eq("user_id", BaseContext.getCurrentId());
        List<AddressBook> addressBooks = addressBookService.list(wrapper);
        if (addressBooks == null || addressBooks.size() <= 0) {
            //如果当前用户没有默认地址 设置当前地址是默认的
            addressBook.setIsDefault(1);
        }
        //设置用户id
        addressBook.setUserId(BaseContext.getCurrentId());
        addressBookService.save(addressBook);
        return R.sucess("添加收货地址成功");
    }

    //查询用户的收货地址列表
    @GetMapping("/list")
    public R<List<AddressBook>> list() {
        //创建条件构造器
        QueryWrapper<AddressBook> wrapper = new QueryWrapper<>();
        wrapper.eq("user_id", BaseContext.getCurrentId());
        wrapper.orderByDesc("is_default");
        //执行查询 返回结果
        return R.sucess(addressBookService.list(wrapper));
    }

    //设置默认地址
    @PutMapping("/default")
    public R<String> setDefault(@RequestBody AddressBook addressBook) {
        //将当前用户的所有地址 都更新成0
        LambdaUpdateWrapper<AddressBook> wrapper = new LambdaUpdateWrapper<>();
        wrapper.eq(AddressBook::getUserId, BaseContext.getCurrentId());
        wrapper.set(AddressBook::getIsDefault, 0);
        //执行更新
        addressBookService.update(wrapper);

        //再将当前传递过来的id的地址 更新成1
        addressBook.setIsDefault(1);
        addressBookService.updateById(addressBook);
        return R.sucess("设置默认地址成功");
    }

    //查询用户默认地址
    @GetMapping("/default")
    public R<AddressBook> getDefault() {
        //创建条件构造器
        QueryWrapper<AddressBook> wrapper = new QueryWrapper<>();
        wrapper.eq("user_id", BaseContext.getCurrentId());
        wrapper.eq("is_default", 1);
        //执行查询
        AddressBook addressBook = addressBookService.getOne(wrapper);
        if (addressBook != null) {
            return R.sucess(addressBook);
        } else {
            return R.error("用户没有默认地址");
        }
    }

    //根据id查询地址信息
    @GetMapping("/{id}")
    public R<AddressBook> getById(@PathVariable("id") Long id){
        return R.sucess(addressBookService.getById(id));
    }

    //修改地址
    @PutMapping
    public R<String> update(@RequestBody AddressBook addressBook){
        addressBookService.updateById(addressBook);
        return R.error("修改成功");
    }

    //删除地址
    @DeleteMapping
    public R<String> delete(Long ids){
        addressBookService.removeById(ids);
        return R.sucess("删除成功");
    }
}
